module.exports = (schema) => {
  return async (ctx, next) => {
    const validationResult = schema.validate(ctx.request.body);
    if (validationResult.error) {
      ctx.status = 400;
      ctx.body = {
        message: 'Validation error',
        errors: validationResult.error.details.map((detail) => detail.message)
      };
      return;
    }
    ctx.validatedBody = validationResult.value;
    await next();
  };
};